package com.un.ebs.sale.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.product.enums.ProductPurchaseTypeEnum;
import com.un.ebs.sale.domain.*;
import com.un.ebs.sale.dto.*;
import com.un.ebs.sale.service.SaleOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/saleOrder")
@Validated
@Api(value = "sale-order", tags = "")
public class SaleOrderController {

    @Autowired
    private SaleOrderService service;

    @PostMapping("add")
    @ApiOperation(value = "添加", notes = "添加")
    public WebResult save(@RequestBody @Validated SaleOrderAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改", notes = "修改")
    public WebResult update(@RequestBody @Validated SaleOrderUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情")
    public WebResult<SaleOrderVo> getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryViewById(id));
    }

    @GetMapping("readOut")
    @ApiOperation(value = "准备出库")
    public WebResult<SaleOutReadyDto> readOut(@RequestParam String contractCode) {
        return WebUtils.success(service.readyOut(contractCode));
    }

    @PostMapping("saleOut")
    @ApiOperation(value = "销售出库")
    public WebResult saleOut(@RequestBody SaleOutReadyDto req) {
        service.saleOut(req);
        return WebUtils.success();
    }

    @PostMapping("change")
    @ApiOperation(value = "合同变更")
    public WebResult saleOut(@RequestParam String contractCode) {
        service.contractChange(contractCode);
        return WebUtils.success();
    }

    @GetMapping("list")
    @ApiOperation(value = "查询")
    public WebResult<IPage<SaleOrderExt>> list(@RequestParam(required = false) String searchText, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.query(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }

    @GetMapping("listItem")
    @ApiOperation(value = "查询明细")
    public WebResult<IPage<SaleOrderItemExt>> listItem(@RequestParam String id, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItem(id, pageSize, pageIndex));
    }

    @PostMapping("listItemByQuery")
    @ApiOperation(value = "明细")
    public WebResult<IPage<SaleOrderItemExt>> listItemByQuery(@RequestBody SaleOrderItemQueryDto req) {
        return WebUtils.success(service.queryItem(req, req.getPageSize(), req.getPageIndex()));
    }

    @PostMapping("listItemByCustomer")
    @ApiOperation(value = "明细")
    public WebResult<IPage<SaleOrderItemExt>> listItemByCustomer(@RequestParam String customerEntId, @RequestParam(required = false) String skuId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItemByCustomer(customerEntId, skuId, pageSize, pageIndex));
    }

    @GetMapping("listDeliverPlan")
    @ApiOperation(value = "查询交付计划")
    public WebResult<IPage<SaleOrderDeliverPlanExt>> listDeliverPlan(@RequestParam(required = false) String searchText, @RequestParam(required = false) ProductPurchaseTypeEnum purchaseTypeEnum, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryDeliverPlan(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), purchaseTypeEnum, pageSize, pageIndex));
    }

    @GetMapping("listByTempContract")
    @ApiOperation(value = "查询订单，按临时合同")
    public WebResult<IPage<SaleOrderExt>> listByTempContract(@RequestParam String customerEntId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryByTempContract(customerEntId, pageSize, pageIndex));
    }

    @GetMapping("listSaleDeliverPlan")
    @ApiOperation(value = "查询订单，具备装箱条件的")
    public WebResult<IPage<SaleOrderReadyDeliverExt>> listSaleDeliverPlan(@RequestParam String searchText, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryReadyDeliver(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }

    @GetMapping("listSaleDeliverItemPlan")
    @ApiOperation(value = "查询订单，具备装箱条件的")
    public WebResult<List<SaleOrderItemExt>> listSaleDeliverItemPlan(@RequestParam String saleContractCode) {
        return WebUtils.success(service.queryReadyDeliverItem(saleContractCode));
    }

    @GetMapping("listSummary")
    @ApiOperation(value = "报表")
    public WebResult<IPage<SaleSummaryExt>> listSummary(@RequestParam String searchText, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.querySummary(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }

    @GetMapping("listSummaryByNote")
    @ApiOperation(value = "报表，按客户查询所有合同汇总信息")
    public WebResult<IPage<SaleContractNoteExt>> listSummaryByNote(@RequestParam String entId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.querySaleContractNote(entId, pageSize, pageIndex));
    }

    @GetMapping("listSummaryItem")
    @ApiOperation(value = "报表")
    public WebResult<IPage<SaleSummaryItemExt>> listSummaryItem(@RequestParam String entId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.querySummaryItem(entId, pageSize, pageIndex));
    }

    @GetMapping("listReportByWeek")
    @ApiOperation(value = "报表，按周统计")
    public WebResult<IPage<SaleReportByWeekExt>> listReportByWeek(@RequestParam String entId, @RequestParam Integer week, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryReportByWeek(entId, week, pageSize, pageIndex));
    }

    @PostMapping("submit/{id}")
    @ApiOperation(value = "提交")
    public WebResult submit(@PathVariable("id") String id) {
        service.submit(id);
        return WebUtils.success();
    }

    @PostMapping("submitCancel/{id}")
    @ApiOperation(value = "提交撤销")
    public WebResult submitCancel(@PathVariable("id") String id) {
        service.submitCancel(id);
        return WebUtils.success();
    }

    @PostMapping("saleFromContract")
    @ApiOperation(value = "合同转销售，返回销售订单")
    public WebResult<SaleOrderVo> saleFromContract(@RequestParam String contractId) {
        //String id = service.saleFromContract(contractId);
        //return WebUtils.success(service.queryViewById(id));
        return WebUtils.success(service.queryViewByContract(contractId));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("cancelItem/{id}")
    @ApiOperation(value = "删除明细")
    public WebResult cancelItem(@PathVariable("id") String id) {
        service.cancelItem(id);
        return WebUtils.success();
    }

    @PostMapping("changeItem")
    @ApiOperation(value = "变更明细")
    public WebResult changeItem(@RequestParam("id") String id, @RequestParam Integer sourceQty, @RequestParam Integer newQty) {
        service.changeItem(id, sourceQty, newQty);
        return WebUtils.success();
    }

    @PostMapping("relationToContract")
    @ApiOperation(value = "关联合同")
    public WebResult relationToContract(@RequestParam String orderId, @RequestParam String contractCode) {
        service.relationToContract(orderId, contractCode);
        return WebUtils.success();
    }
}